﻿package com.tudou.home.newskin.containers
{
    import com.tudou.consts.*;
    import com.tudou.home.newskin.buttons.*;
    import com.tudou.home.newskin.consts.*;
    import com.tudou.home.newskin.events.*;
    import com.tudou.home.newskin.interfaces.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class VideoSprite extends Sprite implements IVideoSize, IRate, IPlayer, IOriginalInfo, IStream, ITransform
    {
        private var isReady:Boolean;
        private var originalInfo:ResolutionTip;
        private var loadingSprite:LoadingSprite;
        private var background:Sprite;
        private var changeRateBMP:Bitmap;
        private var isChangingRate:Boolean = false;
        private var rateType:int;
        private var timer:Timer;
        private var originalRate:Number;
        private var brightness:Number = 0;
        private var sizeType:int = 2;
        private var originalHeight:int;
        private var original:Boolean;
        private var maxRate:int;
        private var video:DisplayObject;
        private var originalWidth:int;
        private var lastClickTime:int;

        public function VideoSprite()
        {
            lastClickTime = -NewSkinConsts.CLICK_INTERVAL;
            background = RectDisplayUtil.getSprite(400, 300, 16711680, 0);
            addChild(background);
            changeRateBMP = new Bitmap();
            changeRateBMP.smoothing = true;
            changeRateBMP.bitmapData = new BitmapData(400, 300, false, 0);
            changeRateBMP.visible = false;
            addChild(changeRateBMP);
            loadingSprite = new LoadingSprite();
            loadingSprite.visible = false;
            addChild(loadingSprite);
            originalInfo = new ResolutionTip();
            originalInfo.x = 20;
            originalInfo.y = 20;
            originalInfo.visible = false;
            addChild(originalInfo);
            isChangingRate = false;
            return;
        }// end function

        private function hideOriginalInfo() : void
        {
            originalInfo.visible = false;
            disposeTimer();
            return;
        }// end function

        public function setEnableClick(param1:Boolean) : void
        {
            if (param1)
            {
                this.addEventListener(MouseEvent.CLICK, clickHandler);
            }
            else
            {
                this.removeEventListener(MouseEvent.CLICK, clickHandler);
            }
            this.buttonMode = param1;
            this.useHandCursor = param1;
            this.mouseChildren = !param1;
            this.doubleClickEnabled = !param1;
            return;
        }// end function

        public function bufferEmpty() : void
        {
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            var _loc_3:Timer = null;
            var _loc_2:* = getTimer();
            if (_loc_2 - lastClickTime <= NewSkinConsts.CLICK_INTERVAL)
            {
                lastClickTime = -NewSkinConsts.CLICK_INTERVAL;
                dispatchEvent(new SkinEvent(SkinEvent.SET_FULLSCREEN));
            }
            else
            {
                _loc_3 = new Timer(NewSkinConsts.CLICK_INTERVAL, 1);
                _loc_3.addEventListener(TimerEvent.TIMER_COMPLETE, waitCompleted);
                lastClickTime = _loc_2;
                _loc_3.start();
            }
            return;
        }// end function

        public function ready() : void
        {
            isReady = true;
            if (rateType == RateType.ORIGINAL)
            {
                showOriginalInfo();
            }
            else
            {
                hideOriginalInfo();
            }
            return;
        }// end function

        private function autoCloseHlr(event:TimerEvent) : void
        {
            hideOriginalInfo();
            return;
        }// end function

        private function createTimer(param1:Number = 5000) : void
        {
            disposeTimer();
            timer = new Timer(param1, 1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, autoCloseHlr);
            timer.start();
            return;
        }// end function

        private function showOriginalInfo() : void
        {
            originalInfo.visible = true;
            createTimer(NewSkinConsts.RESOLUTION_TIP_TIME);
            return;
        }// end function

        public function playing() : void
        {
            return;
        }// end function

        public function loading() : void
        {
            isReady = false;
            isChangingRate = false;
            changeRateBMP.visible = false;
            loadingSprite.visible = false;
            return;
        }// end function

        public function setMaxRate(param1:int, param2:Boolean) : void
        {
            this.maxRate = param1;
            this.original = param2;
            return;
        }// end function

        public function setOriginalWidthHeight(param1:int, param2:int) : void
        {
            originalWidth = param1;
            originalHeight = param2;
            originalInfo.setResolution(param1, param2);
            return;
        }// end function

        public function paused() : void
        {
            return;
        }// end function

        public function completed() : void
        {
            return;
        }// end function

        public function setOriginalRate(param1:Number) : void
        {
            originalRate = param1;
            return;
        }// end function

        private function waitCompleted(event:TimerEvent) : void
        {
            var _loc_2:* = event.target as Timer;
            _loc_2.stop();
            _loc_2.removeEventListener(TimerEvent.TIMER_COMPLETE, waitCompleted);
            if (lastClickTime == -NewSkinConsts.CLICK_INTERVAL)
            {
                return;
            }
            lastClickTime = -NewSkinConsts.CLICK_INTERVAL;
            dispatchEvent(new SkinEvent(SkinEvent.TOGGLE_PAUSE));
            return;
        }// end function

        public function setVideoSize(param1:int) : void
        {
            var _loc_2:Number = NaN;
            sizeType = param1;
            if (sizeType == VideoSizeType.FULL)
            {
                video.x = 0;
                video.y = 0;
                video.width = background.width;
                video.height = background.height;
            }
            else
            {
                _loc_2 = VideoSizeType.getSizeByType(sizeType);
                video.width = background.width * _loc_2;
                video.height = background.height * _loc_2;
                video.x = (background.width - video.width) / 2;
                video.y = (background.height - video.height) / 2;
            }
            return;
        }// end function

        public function getRate() : int
        {
            return rateType;
        }// end function

        public function addVideo(param1:DisplayObject) : void
        {
            var _loc_2:* = getChildIndex(background);
            this.video = param1;
            addChildAt(this.video, _loc_2);
            goTransform(background.width, background.height, false);
            return;
        }// end function

        public function setRate(param1:int) : void
        {
            var type:* = param1;
            rateType = type;
            if (video == null || !isReady)
            {
                return;
            }
            var bmpData:* = new BitmapData(video.width, video.height, false, 0);
            try
            {
                bmpData.draw(video, null, null, null, null, true);
                var _loc_3:int = 1;
                changeRateBMP.scaleY = 1;
                changeRateBMP.scaleX = _loc_3;
                changeRateBMP.bitmapData = bmpData;
                changeRateBMP.visible = true;
                loadingSprite.visible = true;
                loadingSprite.setRateType(rateType);
                isChangingRate = true;
            }
            catch (e:Error)
            {
                trace(e);
            }
            return;
        }// end function

        public function getVideoSize() : int
        {
            return sizeType;
        }// end function

        public function buffering() : void
        {
            return;
        }// end function

        public function bufferFull() : void
        {
            if (isChangingRate)
            {
                changeRateBMP.visible = false;
                loadingSprite.visible = false;
                if (rateType == RateType.ORIGINAL)
                {
                    showOriginalInfo();
                }
                else
                {
                    hideOriginalInfo();
                }
                isChangingRate = false;
            }
            return;
        }// end function

        public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            background.width = param1;
            background.height = param2;
            if (video)
            {
                setVideoSize(sizeType);
            }
            changeRateBMP.width = param1;
            changeRateBMP.height = param2;
            loadingSprite.x = int((param1 - loadingSprite.width) / 2);
            loadingSprite.y = int((param2 - loadingSprite.height) / 2);
            return;
        }// end function

        private function disposeTimer() : void
        {
            if (timer)
            {
                timer.stop();
                timer.removeEventListener(TimerEvent.TIMER_COMPLETE, autoCloseHlr);
                timer = null;
            }
            return;
        }// end function

        public function setVideoVisible(param1:Boolean) : void
        {
            if (video)
            {
                video.visible = param1;
            }
            return;
        }// end function

    }
}
